package com.example.demo.service.impl;

import com.example.demo.common.AppResult;
import com.example.demo.common.ResultCode;
import com.example.demo.dao.UserMapper;
import com.example.demo.exception.ApplicationException;
import com.example.demo.model.User;
import com.example.demo.service.IUserService;
import com.example.demo.utils.MD5Utils;
import com.example.demo.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

//具体的实现类

@Slf4j
@Service
public class UserServiceImpl implements IUserService {

    //注入userMapper
    @Resource
    private UserMapper userMapper;
    @Override
    public User selectByUsername(String username) {
        //做一个非空校验
        if(StringUtils.isEmpty(username)){
            //打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //调用DAO查询
        User user = userMapper.selectByUsername(username);
        //返回结果
        return user;
        //然后进行单元测试
    }

    @Override
    public void createNormalUser(User user) {
        // 1.非空校验
        if(user == null
                || StringUtils.isEmpty(user.getUsername())
                || StringUtils.isEmpty(user.getNickname())
                || StringUtils.isEmpty(user.getPassword())
                || StringUtils.isEmpty(user.getSalt())){
            //打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        // 2.校验用户是否存在，存在抛异常
        User existsUser = selectByUsername(user.getUsername());
        if(existsUser != null){
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_USER_EXISTS));
        }
        // 3.设置默认值
        if(user.getGender() == null || user.getGender() < 0 || user.getGender() > 2){
            //性别保密，2是保密
            user.setGender((byte) 2);
        }
        //发布的文章数量
        user.setArticleCount(0);
        //是否管理员
        user.setIsAdmin((byte) 0);
        //状态
        user.setState((byte) 0);
        //是否删除
        user.setDeleteState((byte) 0);
        //时间
        Date date = new Date();
        //创建时间
        user.setCreateTime(date);
        //更新时间
        user.setUpdateTime(date);

        // 4.写入用户数据，返回结果
        int row = userMapper.insertSelective(user);
        //判断受影响的行数
        if(row != 1){
            log.warn("用户注册时，" + ResultCode.FAILED_CREATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_CREATE));
        }
    }

    @Override
    public User login(String username, String password) {
        // 1.非空校验
        if(StringUtils.isEmpty(username) || StringUtils.isEmpty(password)){
            // 打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //2.查询用户是否存在
        User user = selectByUsername(username);
        //校验用户是否存在
        if(user == null){//用户不存在
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_LOGIN.getMessage()));
        }
        //校验密码
        //获取用户的盐
        String salt = user.getSalt();
        //生成密文
        String ciphertext = MD5Utils.md5Salt(password,salt);
        //比较密码是否一致
        if(!ciphertext.toLowerCase().equals(user.getPassword().toLowerCase())){
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_LOGIN.getMessage()));
        }
        //验证通过，返回User对象
        return user;
    }
}
